/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import model.LogFile;
import pojo.MyLST;

/**
 *
 * @author A7med Kotb
 */
public class LiveServersTaskDao 
{
    Connection con;

    public LiveServersTaskDao(Connection con) 
    {
        this.con = con;
    }
    
    //method to insert live server task data
    public boolean insert(MyLST lst)
    {
        boolean check=false;
        LiveServersDao lsd=new LiveServersDao(con);
        try 
        {
          PreparedStatement ps= con.prepareStatement("insert into liveserverstask values ( ? , ? , ? , ? , ? , ? , ? , ? , ? )");
          ps.setInt(1, lst.getLstId());
          ps.setString(2, lst.getSchedule());
          ps.setString(3, lst.getModifier());
          ps.setDate(4, lst.getStartDate());
          ps.setString(5, lst.getStartTime());
          ps.setString(6,lst.getDay());
          ps.setBoolean(7, lst.getIsEnable());
          ps.setBoolean(8,lst.getIsRunning());
          ps.setString(9,lst.getMonth());
          int i=ps.executeUpdate();
          if(i == 1)
          {
              LogFile.log(" System ", " Successffully saved all Schdule Task data related to Live Server with ID :["+lsd.getById(lst.getLstId()).getIp()+"], to its table in Database .");
              check=true;
          }else{
              LogFile.log(" System ", " failed to save Schedule Task Data related to Live Server with ID : ["+lsd.getById(lst.getLstId()).getIp()+"] , to its table in Database .");
              check=false;
          }
        } 
        catch (SQLException ex) 
        {
            check=false;
            LogFile.log(" Sytem ", " encountered the following exception while trying to Save Schedule Task Data for live server with IP ["+lsd.getById(lst.getLstId()).getIp()+"],and this is the exception "+ex);
            ex.printStackTrace();
        }
        return check;
    }
    
    //method to update live server task data
    public boolean update(MyLST lst)
    {
        LiveServersDao lsd=new LiveServersDao(con);
        boolean check=false;
        try 
        {
            boolean first=false;
            String updatetxt="";
            if(lst.getSchedule()!=null)
            {
                if (first) 
                {
                    updatetxt+=", ";
                }
                first=true;
                updatetxt+="schedule='"+lst.getSchedule()+"'";
            }
            if(lst.getModifier()!=null)
            {
                  if (first) 
                  {
                    updatetxt+=", ";
                  }
                  first=true;
                  updatetxt+="modifier='"+lst.getModifier()+"'";
            }
            if(lst.getStartDate()!=null)
            {
                  if (first) 
                  {
                    updatetxt+=", ";
                  }
                  first=true;
                  updatetxt+="start_date='"+lst.getStartDate()+"'";
            }
            if(lst.getStartTime()!=null)
            {
                  if (first) 
                  {
                    updatetxt+=", ";
                  }
                  first=true;
                  updatetxt+="start_time='"+lst.getStartTime()+"'";
            }
            if(lst.getDay()!=null)
            {
                  if (first) 
                  {
                    updatetxt+=", ";
                  }
                  first=true;
                  updatetxt+="day='"+lst.getDay()+"'";
            }
            if(lst.getMonth()!=null)
            {
                  if (first) 
                  {
                    updatetxt+=", ";
                  }
                  first=true;
                  updatetxt+="month='"+lst.getMonth()+"'";
            }
             if(first)
            { 
                updatetxt+=" , ";
            }
            updatetxt+=" isenable = "+lst.getIsEnable()+" , isrunning = "+lst.getIsRunning()+" ";
            PreparedStatement ps=con.prepareStatement("update liveserverstask set "+updatetxt+" where lst_id=?");
            ps.setInt(1, lst.getLstId());
            int i=ps.executeUpdate();
            if(i == 1)
            {
                check=true;
            }
        } 
        catch (SQLException ex) 
        {
            check=false;
            ex.printStackTrace();
            LogFile.log(" System "," encountered the following Exception while trying to update task data of server "+lsd.getById(lst.getLstId()).getIp()+"and the error is : "+ex.getMessage());
        }
        return check;
    }

    //method to delete live server task from db...
    public boolean deleteById(int lstid)
    {
        LiveServersDao lsd=new LiveServersDao(con);
        boolean check=false;
        try 
        {
            PreparedStatement ps=con.prepareStatement("delete from liveserverstask where lst_id=?");
            ps.setInt(1, lstid);
            int i=ps.executeUpdate();
            if(i == 1)
            {
                check=true;
            }
        } 
        catch (SQLException ex) 
        {
            check=false;
            LogFile.log(" System "," encountered the following Exception while trying to delete the task of server "+lsd.getById(lstid).getIp()+" and this is the error "+ex.getMessage());
            ex.printStackTrace();
        }
        return check;
    }

    //get Live Server task Data by live server ID
    public MyLST getById(int lstid){
        try {
            MyLST lst=new MyLST();
            PreparedStatement ps= con.prepareStatement("select * from liveserverstask where lst_id=?");
            ps.setInt(1, lstid);
            ResultSet rset= ps.executeQuery(); 
            rset.next();
                lst.setLstId(rset.getInt(1));
                lst.setSchedule(rset.getString(2));
                lst.setModifier(rset.getString(3));
                lst.setStartDate(rset.getDate(4));
                lst.setStartTime(rset.getString(5));
                lst.setDay(rset.getString(6));
                lst.setIsEnable(rset.getBoolean(7));
                lst.setIsRunning(rset.getBoolean(8));
                lst.setMonth(rset.getString(9));
            return lst;
        } 
        catch (SQLException ex) 
        {
            ex.printStackTrace();
        }
            return null;
    }  
}
